package university;
/**
 * Class to describe exam process
 * @author anatolii.bondarenko
 */
import university.exceptions.*;

import java.util.*;

class Exam extends KnowledgeControl {
    public Group getGroup(){
        return group;
    }
    private Group group;
    private ArrayList<Student> failedStudents;
    /**
     * Details of exam
     * @param d is discipline of exam(teacher is taken from discipline class)
     * @param gr is group that will be examined
     * @param ev is time, date and place where exam will take part
     */
    public Exam(Discipline d, Group gr, ExamEvent ev) throws GroupGraduatedException{
		super(d, ev);
        group = gr;
        if (gr.isGraduated() == true){
            throw new GroupGraduatedException("Cannot examine already graduated group");
        }
        failedStudents = new ArrayList<Student>(0);
	}

    /**
     * Constructor that is used for exam retries
     * @param d is discipline of exam(teacher is taken from discipline class)
     * @param ev is time, date and place where exam will take part
     */
    public Exam(Discipline d, ExamEvent ev){
        super(d, ev);
    }

    /**
     * Gets all details(Discipline name, time and place, teacher name, group ro be examined) in String format
     * @return details of exam
     */
    public String toString(){
        return "Exam " + super.getDiscipline() + super.getTimeAndPlace() + ". Teacher: "
                + super.getTeacher() + ". Group " + group.getGroupName()+".";
    }

    /**
     * Each student of the group must be examined. Records assessment of student knowledge if student passes the exam.
     * Adds student to failedStudents array if student fails the exam.
     * @param mark is assessment of the exam
     * @param s is the student that is currently being examined
     */
    public void examine(int mark, Student s) throws InvalidMarkException, ExamineResignedStudentException, NoElementInListException, NoFailedDisciplinesException{
		if (group.getListOfStudents().contains(s) == false){
            throw new NoElementInListException("No such student in this group");
        }
        if (s.isResigned()==true){
            throw new ExamineResignedStudentException("Cannot examine resigned students");
        }
        if (mark >= 1 && mark <=5){
			if (mark <=2){
                s.addFailedDiscipline();
                failedStudents.add(s);
			}
			else {
				s.addAssessmentResult(mark);
			}
		}
        else {
            throw new InvalidMarkException("Exam marks must be from 1 to 5");
        }
	}

    /**
     * Gets list of students that failed the exam
     * @return list of students that failed the exam
     */
    public ArrayList<Student> getListOfFailedStudents(){
        return failedStudents;
    }

    /**
     * Gets group that will take the exam
     * @return group that will take the exam
     */

}
